package com.fleafinder;

import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.fleafinder.entity.Market;

public class ListViewMarketListAdapter extends ArrayAdapter<Market> {
	public static List<Market> list;
	private final Activity context;

	public ListViewMarketListAdapter(Activity context, List<Market> list) {
		super(context, R.layout.listview_market_list_layout, list);
		this.context = context;
		ListViewMarketListAdapter.list = list;
	}

	/*
	 * @Override public View getView(int position, View convertView, ViewGroup
	 * parent) { View view = null; if (convertView == null) { LayoutInflater
	 * inflator = context.getLayoutInflater(); view =
	 * inflator.inflate(R.layout.listview_market_list_layout, null); } else {
	 * view = convertView; }
	 * 
	 * Market market = null; try { market = list.get(position); } catch
	 * (IndexOutOfBoundsException ex) { ex.printStackTrace(); }
	 * 
	 * TextView name = (TextView) view.findViewById(R.id.marketlist_name);
	 * name.setText(market.getName()); name.setTag(position);
	 * 
	 * TextView road = (TextView) view.findViewById(R.id.marketlist_address);
	 * road.setText(market.getRoad());
	 * 
	 * TextView city = (TextView) view.findViewById(R.id.marketlist_city);
	 * city.setText(market.getCity()); TextView date = (TextView)
	 * view.findViewById(R.id.marketlist_date);
	 * 
	 * // java.text.DateFormat dateFormat = new SimpleDateFormat("dd.MM.yy"); //
	 * try { Date today = new Date();
	 * 
	 * // Date dateFrom = dateFormat.parse(market.get.getDateFrom()); //Date
	 * dateTo = dateFormat.parse(market.getDateTo());
	 * 
	 * if (today.after(market.getDateFromObject()))// && today.before(dateTo))
	 * date.setText(CommonConstants.TXT_TODAY); else { String dateStr = "d. " +
	 * market.getDateFrom() + " - " + market.getDateTo(); date.setText(dateStr);
	 * } // } catch (ParseException e) { // date.setText(dateStr); // }
	 * 
	 * String timeStr = "Tid: " + market.getStartAt() + " - " +
	 * market.getEndAt();
	 * 
	 * TextView time = (TextView) view.findViewById(R.id.marketlist_time);
	 * time.setText(timeStr); return view; }
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {

			LayoutInflater inflator = context.getLayoutInflater();
			convertView = inflator.inflate(
					R.layout.listview_market_list_layout, null);

			holder = new ViewHolder();
			holder.name = (TextView) convertView
					.findViewById(R.id.marketlist_name);
			holder.road = (TextView) convertView
					.findViewById(R.id.marketlist_address);
			holder.city = (TextView) convertView
					.findViewById(R.id.marketlist_city);
			holder.date = (TextView) convertView
					.findViewById(R.id.marketlist_date);
			holder.time = (TextView) convertView
					.findViewById(R.id.marketlist_time);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Market market = null;

		try {
			market = list.get(position);
		} catch (IndexOutOfBoundsException ex) {
			ex.printStackTrace();
		}

		holder.name.setText(market.getName());
		holder.name.setTag(position);
		holder.road.setText(market.getRoad());
		holder.city.setText(market.getCity());

		Date today = new Date();
		if (today.after(market.getDateFromObject()))// && today.before(dateTo))
			holder.date.setText(CommonConstants.TXT_TODAY);
		else {
			String dateStr = "d. " + market.getDateFrom() + " - "
					+ market.getDateTo();
			holder.date.setText(dateStr);
		}

		String timeStr = "Tid: " + market.getStartAt() + " - "
				+ market.getEndAt();
		holder.time.setText(timeStr);
		
		return convertView;
	}

	// View Holder class
	public static class ViewHolder {
		TextView name;
		TextView road;
		TextView city;
		TextView date;
		TextView time;
	}
}
